using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DABL;
using WebChart;
public partial class Control_Results : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.QueryString["ID"] != null)
        {
            if(!IsPostBack)
            {
                string QuestionnaireID = Convert.ToString(Request.QueryString["ID"]);
                litQuestionnaire.Text = QuestionnaireController.QuestionnairesGetByID(QuestionnaireID).Tables[0].Rows[0]["QuestionnaireName"].ToString().Replace(Environment.NewLine, "<br>");
                rptResults.DataSource = QuestionnaireController.QuestionsGetByQuestionnaireID(QuestionnaireID);
                rptResults.DataBind();
            }
        }
    }
    protected void rptResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Int32 QuestionID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "QuestionID"));
            Int32 QuestionType_FK = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "QuestionType_FK"));
            Literal litQuestion = (Literal)e.Item.FindControl("litQuestion");
            litQuestion.Text = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "Question"));
            if(QuestionType_FK == 1)
            {
                BulletedList bList = new BulletedList();
                bList.DataTextField = "Answer";
                bList.DataValueField = "AnswerID";
                bList.DataSource = QuestionnaireController.AnswersOpendEndedResults(QuestionID).Tables[0].DefaultView;
                bList.DataBind();
                e.Item.Controls.Add(bList);

            }
            else if(QuestionType_FK == 2)
            {
                DataTable dt = QuestionnaireController.AnswersLikertScaleResults(QuestionID).Tables[0];
                ChartControl cc = (ChartControl)e.Item.FindControl("ChartControl1");
                cc.Visible = true;
                ColumnChart colChart = new ColumnChart();

                ChartPoint CPts = new ChartPoint("1", Convert.ToInt32(dt.Rows[0]["1"]));
                colChart.Data.Add(CPts);
                CPts = new ChartPoint("2", Convert.ToInt32(dt.Rows[0]["2"]));
                colChart.Data.Add(CPts);
                CPts = new ChartPoint("2", Convert.ToInt32(dt.Rows[0]["3"]));
                colChart.Data.Add(CPts);
                CPts = new ChartPoint("3", Convert.ToInt32(dt.Rows[0]["4"]));
                colChart.Data.Add(CPts);
                CPts = new ChartPoint("5", Convert.ToInt32(dt.Rows[0]["5"]));
                colChart.Data.Add(CPts);
                colChart.DataBind();
                colChart.Shadow.Visible = true;
                colChart.DataLabels.Visible = true;
                //cc.XTitle.Text = "good";
                //cc.YTitle.Text = "bad";
                cc.Charts.Add(colChart);
                cc.RedrawChart();
            }
            else if(QuestionType_FK == 3)
            {

                ChartControl cc = (ChartControl)e.Item.FindControl("ChartControl1");
                cc.Visible = true;
                PieChart colChart = new PieChart();
                colChart.DataSource = QuestionnaireController.AnswersMCQsResults(QuestionID).Tables[0].DefaultView;
                colChart.DataXValueField = "Answer";
                colChart.DataYValueField = "OptionId_Fk";

                colChart.DataBind();
                colChart.DataLabels.Visible = true;
                colChart.Shadow.Visible = true;
                colChart.ShowLegend = true;
                colChart.DataLabels.ShowXTitle = true;
                cc.HasChartLegend = true;
                cc.Charts.Add(colChart);

                cc.RedrawChart();
            }





        }
    }
}
